package com.itboye.core.utils;

import android.content.Context;
import android.graphics.Bitmap;
import android.util.Log;
import android.widget.ImageView;

import com.itboye.mengwu.R;
import com.itboye.mengwu.main.MainActivity;
import com.squareup.picasso.Picasso;
import com.squareup.picasso.Transformation;

public class XPicasso {

	public static void loadImage(Context context, ImageView imageView,
			String url) {
		Picasso.with(context).load(url).placeholder(R.drawable.default_image)
				.error(R.drawable.default_image).transform(transformation)
				.into(imageView);
	}

	public static void loadBigImage(Context context, ImageView imageView,
			String url) {
		Picasso.with(context).load(url).placeholder(R.drawable.default_image)
				.error(R.drawable.default_image).transform(transformationBig)
				.into(imageView);
	}

	static Transformation transformation = new Transformation() {

		@Override
		public Bitmap transform(Bitmap source) {

			int targetWidth = MainActivity.WIDTH;
			Log.e("width", "source.getHeight()=" + source.getHeight()
					+ ",source.getWidth()=" + source.getWidth()
					+ ",targetWidth=" + targetWidth);

			if (source.getWidth() == 0) {
				return source;
			}
			// ByAlert.alert(source.getWidth() + ",targetWidth=" + targetWidth);
			// 如果图片小于设置的宽度，则返回原图
			if (source.getWidth() < targetWidth) {
				return source;
			} else {
				// 如果图片大小大于等于设置的宽度，则按照设置的宽度比例来缩放
				double aspectRatio = (double) source.getHeight()
						/ (double) source.getWidth();
				int targetHeight = (int) (targetWidth * aspectRatio);
				if (targetHeight != 0 && targetWidth != 0) {
					Bitmap result = Bitmap.createScaledBitmap(source,
							targetWidth, targetHeight, false);
					if (result != source) {
						// Same bitmap is returned if sizes are the same
						source.recycle();
					}
					return result;
				} else {
					return source;
				}
			}

		}

		@Override
		public String key() {
			return "transformation" + " desiredWidth";
		}
	};

	static Transformation transformationBig = new Transformation() {

		@Override
		public Bitmap transform(Bitmap source) {

			int targetWidth = MainActivity.WIDTH;
			Log.e("width", "source.getHeight()=" + source.getHeight()
					+ ",source.getWidth()=" + source.getWidth()
					+ ",targetWidth=" + targetWidth);

			if (source.getWidth() == 0) {
				return source;
			}
			// ByAlert.alert(source.getWidth() + ",targetWidth=" + targetWidth);
			// 如果图片小于设置的宽度，则返回原图
			// if (source.getWidth() < targetWidth) {
			// //if (tr) {
			// //
			// //}
			//
			// return source;
			// } else {
			// 如果图片大小大于等于设置的宽度，则按照设置的宽度比例来缩放
			double aspectRatio = (double) source.getHeight()
					/ (double) source.getWidth();
			int targetHeight = (int) (targetWidth * aspectRatio);
			if (targetHeight != 0 && targetWidth != 0) {
				Bitmap result = Bitmap.createScaledBitmap(source, targetWidth,
						targetHeight, false);
				if (result != source) {
					// Same bitmap is returned if sizes are the same
					source.recycle();
				}
				return result;
			} else {
				return source;
			}
		}

		// }

		@Override
		public String key() {
			return "transformation" + " desiredWidth";
		}
	};
}
